# coding=utf-8
from flask_restful import Resource
from flask_restful.reqparse import RequestParser
from comment.modules.funding_not_matched import Funding_not_matched
from comment.ultils.decorators import Login_required
from financial.resorces.transaction.serializer import AllMatchedPaginateSerializer

class AllMatchedResource(Resource):
    '''拦截器看用户有没有登录过'''
    # 拦截器他会在LoginOut的所有的视图函数都会在执行视图函数之前，做一下装饰器的拦截
    method_decorators = [Login_required]  # 使用了登录拦截的装饰器

    '''所有待匹配资金列表和所有已经匹配资金列表'''
    def get(self):

        rp = RequestParser()

        rp.add_argument('curPage') #当前页
        rp.add_argument('perPage') #每页数量
        rp.add_argument('type') #匹配状态，如果传入的是2，则查询所有的已经匹配的列表，未匹配则不用传

        args = rp.parse_args()

        cur_page = int(args.get('curPage'))
        per_page = int(args.get('perPage'))
        status = int(args.get('type'))

        if not status:
            #查询所有未匹配和部分匹配
            matched_list = Funding_not_matched.query.filter(Funding_not_matched.matchedStatus != 2)
        else:
            matched_list = Funding_not_matched.query.filter(Funding_not_matched.matchedStatus == 2)

        matched_list = matched_list.paginate(cur_page,per_page,error_out=False)

        data = AllMatchedPaginateSerializer(matched_list).to_dict()

        return {
            'msg':'success',
            'date':data
        }
